/*
 * CompatibilityDescriptor.java - ScriptME
 * 
 * Copyright (c) 2009 Cesar Henriques <cesar at alttab.com.ar>.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser Public License v2.1
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * 
 * Based on FESI Project
 * 
 * Contributors:
 * 	Jean-Marc Lugrin - initial API and implementation
 * 	Cesar Henriques <cesar at alttab.com.ar> - J2ME Porting and Extensions
 */
package org.scriptme.ecmascript.data;

// TODO: Auto-generated Javadoc
/**
 * Descripe the compatibility of a method, allowing to postpone the choice of
 * the method to call until all potentially compatible ones have been examined.
 */
class CompatibilityDescriptor {

	/** The distance. */
	int distance;

	/** The convert to char. */
	boolean[] convertToChar = null; // flag to indicate if conversion to char is
									// needed

	/** The converted arrays. */
	Object[] convertedArrays = null; // Converted array if any needed

	/*
	 * Create a new descrptor (note that there is a predefined one for
	 * incompatible parameters). @param distance -1 if incompatible, or
	 * evaluation of conversion distance @param convertToChar Optional flag for
	 * each parameter @param convertedArrays Optional list of converted arrays
	 */
	/**
	 * Instantiates a new compatibility descriptor.
	 * 
	 * @param distance
	 *            the distance
	 * @param convertedToChar
	 *            the converted to char
	 * @param convertedArrays
	 *            the converted arrays
	 */
	CompatibilityDescriptor(int distance, boolean[] convertedToChar,
			Object[] convertedArrays) {
		this.distance = distance;
		if (distance >= 0) {
			this.convertToChar = convertedToChar;
			this.convertedArrays = convertedArrays;
		}
	}

	/**
	 * Return true if the method is compatible with the parameters.
	 * 
	 * @return true, if checks if is compatible
	 */
	public boolean isCompatible() {
		return distance >= 0;
	}

	/**
	 * Return the distance between perfect compatibility and this one.
	 * 
	 * @return the distance
	 */
	public int getDistance() {
		return distance;
	}

	/**
	 * Convert the parameters for this method call.
	 * 
	 * @param params
	 *            the params
	 */
	public void convert(Object params[]) {
		if (params != null && distance >= 0) {
			// Modify the parameters
			int n = params.length;

			if (convertToChar != null) {
				for (int i = 0; i < n; i++) {
					if (convertToChar[i]) {
						String s = (String) params[i];
						Character c = new Character(s.charAt(0));
						params[i] = c;
					}
				}
			}
			if (convertedArrays != null) {
				for (int i = 0; i < n; i++) {
					if (convertedArrays[i] != null) {
						params[i] = convertedArrays[i];
					}
				}
			}
		}
	} // convert
} // class CompatibilityDescriptor
